<template>
  <div class="schoollist">
        <div style="font-size:18px;">学校管理管理</div>
        <hr>
        <br/>
        <div style="float: right;margin-bottom: 15px;">   
            <el-button  @click="openAddDiolog">添加学校</el-button>
        </div>
         <el-table :data="schoolList" border style="width: 100%"  >
            <!-- <el-table-column type="selection" width="auto" ></el-table-column> -->
            <el-table-column prop="name" label="名称" ></el-table-column>
            <el-table-column prop="schoolCode" label="编号" ></el-table-column>
            <el-table-column label="操作" >
                <template slot-scope="scope">
                    <el-button size="small" @click="updataSchool(scope.row)">修改信息</el-button>
                    <el-button size="small" type="danger" @click="delSchoolList(scope.row.id,scope.$index)">删除学校</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="添加学校" v-model="showDialog" size="large" style="width:80%;margin:0 auto;max-width:400px;top:0%;">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="学校名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
              <el-form-item label="学校编号" prop="name">
                <el-input v-model="ruleForm.schoolCode"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="shoolSava('ruleForm')">保存</el-button>
                <el-button @click="showDialog=false">取消</el-button>
              </el-form-item>
            </el-form>
        </el-dialog>
        <el-dialog title="修改学校" v-model="showDialogs" size="large" style="width:80%;margin:0 auto;max-width:400px;top:0%;">
            <el-form :model="ruleForms" :rules="rules" ref="ruleForms" label-width="100px" class="demo-ruleForm">
              <el-form-item label="学校名称" prop="name">
                <el-input v-model="ruleForms.name"></el-input>
              </el-form-item>
              <el-form-item label="学校编号" prop="name">
                <el-input v-model="ruleForms.schoolCode"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updataSave('ruleForms')">保存</el-button>
                <el-button @click="showDialogs=false">取消</el-button>
              </el-form-item>
            </el-form>
        </el-dialog>
  </div>
  
</template>
<script>
export default {
  data(){
    return{
     schoolList:[],
    showDialog:false,
    showDialogs:false,
    ruleForms: {
          name: '',
          id:'',
          schoolCode: '',
        },
    ruleForm: {
          name: '',
          schoolCode: '',
        },
        rules: {
          name: [
            { required: true, message: '请输入学校名称', trigger: 'blur' }
          ],
          schoolCode: [
            { required: true, message: '请输入学校编号', trigger: 'blur' }
          ]
        }
    }
  },
   mounted(){
     this.getSchoolList()
  },
  methods:{
     getSchoolList(){   
        var _self = this;
       _self.myajax("/school/list" ,{},function(res){
			console.log(res)
           _self.schoolList=res.data.list
		})
     },
     delSchoolList(id,index){   //删除学校
        var _self = this;
        let data={
          id:id
        }
       _self.myajax("/school/delete",data,function(res){
          console.log(res)
          if(res.code==1000){
              _self.$notify({title: '提示',message: "删除成功",type: 'success'});
              _self.getSchoolList()
          }
	    	})
     },
     openAddDiolog() {
        this.showDialog = true
        this.ruleForm.id = ''
        this.ruleForm.name = ''
        this.ruleForm.schoolCode = ''
     },
     shoolSava(formName){     //新增学校
         var _self = this;
         this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm.name)
            var data={
              name: this.ruleForm.name,
              schoolCode: this.ruleForm.schoolCode
            }
              _self.myajax("/school/save",data,function(res){
                console.log(res)
                if(res.code==1000){
                   _self.$refs[formName].resetFields();
                  _self.showDialog=false
                  _self.$notify({title: '提示',message: "添加成功",type: 'success'});
                  _self.getSchoolList()
                }
	    	   })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        
     },
     updataSchool(row){
         var _self = this;
          this.showDialogs=true
          this.ruleForms.name=row.name
          this.ruleForms.id=row.id
          this.ruleForms.schoolCode=row.schoolCode
     },
     updataSave(formNames){
        var _self = this;
        let data={
          id: this.ruleForms.id,
          name: this.ruleForms.name,
          schoolCode: this.ruleForms.schoolCode,
        }
         _self.myajax("/school/updateInfo",data,function(res){
                console.log(res)
                if(res.code==1000){
                  _self.$notify({title: '提示',message: "修改成功",type: 'success'});
                  _self.$refs[formNames].resetFields();
                  _self.showDialog=false
                  _self.getSchoolList()
                  _self.showDialogs=false
                }
	    	   })
     }
  }
}
</script>
<style scoped>

</style>


